<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>compose</title>
</head>
<body>
	
<script>
	function compose() {
		
		var fns = [].slice.call(arguments)
		var len = fns.length - 1
		

		function wrap() {
			var args = [].slice.call(arguments)
			var call = fns[len]
			var  result = call.apply(null, args);
			// console.log(call)
			while(len >= 1) {
				len--
				result = fns[len].call(null, result)
				
			}

			return result
		}

		

		return wrap
	}

	var fn1 = function(x) {
		console.log('fn1')
		return 'fn1' + ";" + x;
	}

	var fn2 = function(x) {
		console.log('fn2')
		return 'fn2' + ';' + x
	}

	var getJson = function(x) {
		console.log('url: ' + x)
		return x + ';url'
	}

	var c = compose(fn1, fn2)
	// console.log(c('a'))

	var toUpperCase = function(str) {
		return str.toUpperCase()
	}

	var exclaim = function(x) {
		return x + '!'
	}

	

	var shout = compose(exclaim, getJson, toUpperCase)
	console.log(shout('kubi'))
</script>
</body>
</html>